//
//  ContactsViewController.m
//  聊吧
//
//  Created by 刘洋 on 16/6/13.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "ContactsViewController.h"
#import "AddFriendViewController.h"
#import "NewFriendViewController.h"

static NSString *contactsCell = @"contactsCell";

@interface ContactsViewController ()<UITableViewDelegate,UITableViewDataSource,EMChatManagerDelegate>

@property(nonatomic,retain)BaseTableView *contactsTableView;
@property(nonatomic,retain)NSMutableArray *friendsList;



@end

@implementation ContactsViewController

-(void)dealloc{
    [_contactsTableView release];
    [_friendsList release];
    [super dealloc];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    
    //从服务器上获取好友数组
    [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
        
        if (!error) {
            NSLog(@"获取成功 %@",buddyList);
            [self.friendsList removeAllObjects];
            [self.friendsList addObjectsFromArray:buddyList];
            [self.contactsTableView reloadData];
        }
        
    } onQueue:dispatch_get_main_queue()];
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"联系人";
    
    self.friendsList = [NSMutableArray array];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加好友" style:UIBarButtonItemStylePlain target:self action:@selector(addFriendButton)];
    
    self.contactsTableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, ROOTWIDTH, ROOTHEIGHT - naVCHeight - tabBarHeight) style:UITableViewStylePlain];
    self.contactsTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.contactsTableView];
    [_contactsTableView release];
    
    self.contactsTableView.delegate = self;
    self.contactsTableView.dataSource = self;
    
    [self.contactsTableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:contactsCell];
    
    
    
    // Do any additional setup after loading the view.
}

#pragma mark -- 添加好友按钮
-(void)addFriendButton{
    
    AddFriendViewController *addVC = [[AddFriendViewController alloc]init];
    addVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addVC animated:YES];
    [addVC release];
    
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 10;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contactsCell];
    cell.textLabel.text = @"111";
//    NSLog(@"%@",self.friendsList);
    return cell;
    
    
    
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"22222";
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        NewFriendViewController *newVC = [[NewFriendViewController alloc]init];
        newVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:newVC animated:YES];
        [newVC release];
    }
    
    
}



-(void)didAcceptedByBuddy:(NSString *)username{
    
    NSLog(@"11111");
    
    NSString *message = [NSString stringWithFormat:@"%@同意您的好友申请",username];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}


- (void)didRejectedByBuddy:(NSString *)username{
    
    
    NSLog(@"22222");
    
    NSString *message = [NSString stringWithFormat:@"%@拒绝了您的好友申请",username];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
